package com.key.service.impl;

import com.github.pagehelper.PageHelper;
import com.key.common.dto.RepairFormFindDTO;
import com.key.common.vo.ClearFormVO;
import com.key.common.vo.RepairFormVO;
import com.key.entity.ClearForm;
import com.key.entity.RepairForm;
import com.key.mapper.AssetsMapper;
import com.key.mapper.RepairFormMapper;
import com.key.service.RepairFormService;
import com.key.utils.ExcelExportHelper;
import com.key.utils.OddNumbersUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wei
 * @create 2023-02-22 9:24
 */
@Service
public class RepairFormServiceImpl implements RepairFormService {

    @Autowired
    private RepairFormMapper repairFormMapper;

    @Autowired
    private AssetsMapper assetsMapper;

    @Override
    @Transactional
    public boolean addRepairForm(RepairForm repairForm) {
        String rfNo = OddNumbersUtils.creatON("WX");

        List<Long> assetsIdList = repairForm.getAssetsIdList();

        List<Integer> assetsStateList = assetsMapper.findAssetsStateByAssetsId(assetsIdList);

        boolean flag = true;

        for(Integer assetsState : assetsStateList){
            if(assetsState != 5){
                flag = false;
                break;
            }
        }

        int i = 0, j = 0, k = 0;

        if(flag){
            repairForm.setRfNo(rfNo);
            //设置状态为未处理
            repairForm.setRfState(1);

            i = repairFormMapper.insertRepairForm(repairForm);
            for(Long assetsId : assetsIdList){
                j = repairFormMapper.insertAssetsRepair(rfNo, assetsId);
            }

            //设置维修单的资产状态为报修
            k = repairFormMapper.updateAssetsState(rfNo, 6);
        }

        return i > 0 && j > 0 && k > 0;
    }

    @Override
    public boolean removeRepairForm(String rfNo) {

        int i = repairFormMapper.deleteAssetsRepairByRfNo(rfNo);

        int j = repairFormMapper.deleteRepairFormByRfNo(rfNo);

        return i > 0 && j > 0;
    }

    @Override
    public boolean editRepairForm(RepairForm repairForm) {
//        String rfNo = repairForm.getRfNo();
//        List<Long> assetsIdList = repairForm.getAssetsIdList();

        int i = repairFormMapper.updateRepairForm(repairForm);
//        int j = 0, k = 0;

//        if(i != 0){
//            j = repairFormMapper.deleteAssetsRepairByRfNo(rfNo);
//            for(Long assetsId : assetsIdList){
//                k = repairFormMapper.insertAssetsRepair(rfNo, assetsId);
//            }
//        }

        return i > 0;
    }

    @Override
    public Map<String, Object> findRepairForm(RepairFormFindDTO repairFormFindDTO) {

        Map<String, Object> map = new HashMap<>();

        List<RepairForm> repairFormList;

        if(repairFormFindDTO.getCurrentPage() != null && repairFormFindDTO.getPageSize() != null){
            //分页
            PageHelper.startPage(repairFormFindDTO.getCurrentPage(), repairFormFindDTO.getPageSize());
            repairFormList = repairFormMapper.selectRepairForm(repairFormFindDTO);
        }else {
            //不分页
            repairFormList = repairFormMapper.selectRepairForm(repairFormFindDTO);
        }

        Integer count = repairFormMapper.getRepairFormCount(repairFormFindDTO);

        map.put("repairFormList", repairFormList);
        map.put("count", count);

        return map;
    }

    @Override
    public RepairFormVO findRepairFormInfoByRfNo(String rfNo) {

        RepairFormVO repairFormVO = repairFormMapper.selectRepairFormByRfNo(rfNo);

        repairFormVO.setAssetsList(repairFormMapper.selectAssetsByRfNo(rfNo));

        return repairFormVO;
    }

    @Override
    public boolean setAssetsState(String rfNo, Integer assetsState, List<Long> assetsIds) {

        int i = repairFormMapper.updateAssetsStateById(rfNo, assetsState, assetsIds);

        return i > 0;
    }

    @Override
    public HSSFWorkbook getWorkBook() {
        String[] header = {"维修信息主键ID", "维修单号", "办理状态(1:未处理，2:已处理)", "照片", "业务日期", "处理人","报修人","维修花费",
        "备注","维修内容"};
        String[] properties = {"rfId", "rfNo", "rfState", "rfImage", "rfBusinessTime", "rfProcessor", "rfReportRepair",
                "rfRepairSpend", "rfRemark", "rfRepairDetails"};
        List<RepairForm> repairFormList = repairFormMapper.selectEndRepairFormList();
        List list = new ArrayList();
        for (RepairForm repairForm : repairFormList) {
            list.add(repairForm);
        }
        ExcelExportHelper excelHelper = new ExcelExportHelper();
        HSSFWorkbook workbook = excelHelper.exportExcel(header, properties, list, "维修信息登记列表");
        return workbook;
    }
}
